package br.com.j7.jdesk.ui.bean;

import java.util.Date;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

import br.com.j7.jdesk.library.exception.RepositoryException;
import br.com.j7.jdesk.persistence.entity.Customers;
import br.com.j7.jdesk.persistence.entity.Services;
import br.com.j7.jdesk.persistence.entity.ServicesAnnotations;
import br.com.j7.jdesk.persistence.entity.ServicesLogs;
import br.com.j7.jdesk.persistence.entity.ServicesSituations;
import br.com.j7.jdesk.persistence.entity.ServicesTypes;
import br.com.j7.jdesk.persistence.repository.CustomerRepository;
import br.com.j7.jdesk.persistence.repository.ServiceLogRepository;
import br.com.j7.jdesk.persistence.repository.ServiceRepository;
import br.com.j7.jdesk.persistence.repository.ServiceSituationRepository;
import br.com.j7.jdesk.persistence.repository.ServiceTypeRepository;
import br.com.j7.jdesk.persistence.repository.ServiceTypeRepositoryTest;
import br.com.j7.jdesk.persistence.repository.impl.CustomerRepositoryImpl;
import br.com.j7.jdesk.persistence.repository.impl.ServiceLogsRepositoryImpl;
import br.com.j7.jdesk.persistence.repository.impl.ServiceRepositoryImpl;
import br.com.j7.jdesk.persistence.repository.impl.ServiceSituationRepositoryImpl;
import br.com.j7.jdesk.persistence.repository.impl.ServiceTypeRepositoryImpl;

/**
 * 
 * @author Edivando Alves
 * 
 */
@ManagedBean
@RequestScoped
public class ServiceBean extends WebContext implements Bean{

	private Services service;
	private ServicesAnnotations serviceAnnotation;
	private ServicesLogs serviceLog;
	
	private List<Services> 					services;
	private List<ServicesTypes> 			serviceTypes;
	private List<ServicesSituations> 		serviceSituations;
	private List<ServicesLogs> 				servicesLogs;
	private List<Customers> 				customers;
	
	private ServiceRepository 			serviceRepository;
	private ServiceTypeRepository 		serviceTypeRepository;
	private ServiceSituationRepository 	serviceSituationRepository;
	private CustomerRepository 			customerRepository;
	private ServiceLogRepository		serviceLogRepository;

	
	public ServiceBean() {
		serviceRepository 	  = new ServiceRepositoryImpl();
		serviceLogRepository  = new ServiceLogsRepositoryImpl();
		serviceTypeRepository = new ServiceTypeRepositoryImpl();
		serviceSituationRepository 	= new ServiceSituationRepositoryImpl();
		customerRepository = new CustomerRepositoryImpl();
		
		service = new Services();
		service.setServiceSituation(new ServicesSituations(1));
		serviceAnnotation = new ServicesAnnotations();
		serviceLog = new ServicesLogs();	
	}

	public String adicionar() {
		try {	
			ServicesLogs serviceLogEnd = null;
			boolean action = false;
			serviceAnnotation.setDsServiceAnnotation(service.getAnnotation());
			serviceLog.addServiceAnnotation(serviceAnnotation);
			serviceLog.setDtBegin(new Date());
			serviceLog.setService(service);
			serviceLog.setUser(getSessionUser());
			serviceLog.setServiceSituation(service.getServiceSituation());
			
			serviceAnnotation.setServiceLog( serviceLog );
			serviceAnnotation.setUser( getSessionUser() );
			
			service.setEnterprise(getSessionEnterprise());
			service.setUser(getSessionUser());
			if(service.getIdService() == null || service.getIdService().equals(0)){
				service.setIdService(null);
				action = true;
			}else {
				List<ServicesLogs> servicesLogs = serviceLogRepository.listServicesLogByService(service);
				if( servicesLogs.size() > 0 ){
					serviceLogEnd = servicesLogs.get( servicesLogs.size() - 1 );
					serviceLogEnd.setDtEnd( new Date() );
				}	
			}

			serviceRepository.addService(service, serviceLog, serviceLogEnd, serviceAnnotation);
			if( action ){
				addMessage("ui.mobile.service.add", service.getCdService());
			}else{
				addMessage("ui.mobile.service.update", service.getCdService());
			}
			service = new Services();
			serviceLog = new ServicesLogs();
			serviceAnnotation = new ServicesAnnotations();
			services = null;
			return "service";
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		
	}
	
	public void adicionarServiceAnnotation(){
		serviceLog.addServiceAnnotation(serviceAnnotation);
		serviceLog.setDtBegin(new Date());
		serviceLog.setService(service);
		serviceLog.setUser(getSessionUser());
		serviceLog.setServiceSituation(service.getServiceSituation());
		
//		serviceAnnotation.setServiceLog(serviceLog);
//		serviceAnnotation.setUser(getSessionUser());
		
	}

	@Override
	public String remover() {
		try {
			if( serviceRepository.remove(service) ){
				addMessage("ui.mobile.service.remove", service.getCdService());
				services = null;
				service = null;
			}
			return "service";
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	public List<Services> getServices() {
		try {
			if(this.services == null){
				this.services = serviceRepository.listByEnterprise(getSessionEnterprise());
			}
			return services;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	public List<ServicesTypes> getServiceTypes() {
		try {
			if (this.serviceTypes == null) {
				this.serviceTypes = serviceTypeRepository.listByEnterprise(getSessionEnterprise());
			}
			return serviceTypes;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	public List<ServicesSituations> getServiceSituations() {
		try {
			if(this.serviceSituations == null){
				this.serviceSituations = serviceSituationRepository.listByEnterprise(getSessionEnterprise());
			}
			return serviceSituations;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	public List<Customers> getCustomers() {
		try {
			if(this.customers == null){
				this.customers = customerRepository.listByEnterprise(getSessionEnterprise());
			}
			return customers;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	public Services getService() {
		return service;
	}
	
	public void setService(Services service) {
		this.service = service;
		try {
			if( servicesLogs == null){
				servicesLogs = serviceLogRepository.listServicesLogByService(this.service);
			}
		} catch (RepositoryException e) {
			throw new RuntimeException(e);
		}
	}
	
	public ServicesAnnotations getServiceAnnotation() {
		return serviceAnnotation;
	}
	
	public void setServiceAnnotation(ServicesAnnotations serviceAnnotation) {
		this.serviceAnnotation = serviceAnnotation;
	}
	
	public ServicesLogs getServiceLog() {
		return serviceLog;
	}
	
	public void setServiceLog(ServicesLogs serviceLog) {
		this.serviceLog = serviceLog;
	}
	
	public List<ServicesLogs> getServicesLogs() {
		return servicesLogs;	
	}
	
	public void setServicesLogs(List<ServicesLogs> servicesLogs) {
		this.servicesLogs = servicesLogs;
	}
	
	@Override
	public String toForm(){
		return "serviceForm";
	}

	@Override
	public String toList() {
		service = new Services();
		return "service";
	}
}
